import React, { Component } from 'react';
import { MoreOutline, LeftOutline ,TextDeletionOutline,EditSOutline} from 'antd-mobile-icons'
import services from '../api/interface'
import {  Toast } from 'antd-mobile'
import '../style/Index/Address/address.css'
class Address extends Component {
    state = {
        listadd: []
    }
    onClick() {
        this.props.history.push('/index/mine')
    }
    ontitle(item){
        this.props.history.push({pathname:'/order',state:{Add:item.province + item.city + item.county + item.addressDetail}})
    }

    async componentDidMount() {
        var res = await services.address.address_list({ token: localStorage.getItem('token'), userid: localStorage.getItem('userid') })
        this.setState({
            listadd: res.data.data
        })
        console.log(res.data.data);
        //查询默认地址
        await services.address.defaultAddress({ token: localStorage.getItem('token'), userid: localStorage.getItem('userid') })
    }
    //删除地址
    async onDeleteadd(item){

        var it=await services.address.address_delete({token:localStorage.getItem('token'),addressid:item.addressid})
        if (it.data.code == 200) {
            Toast.show({
                icon: 'success',
                content: '删除成功！',
            })
        }
        var ress=await services.address.address_list({ token: localStorage.getItem('token'), userid: localStorage.getItem('userid') })
        this.setState({
            listadd:ress.data.data
        })
    }
    onaddress_add() {
        this.props.history.push('/addDetail')
    }
    //修改地址
    onfixadd(item,index){
        var data=this.state.listadd[index]
        this.props.history.push({pathname:'/addDetail',state:{data,addressid:item.addressid}})
        // console.log(item.addressid);
    }
    render() {
        return (
            <div className='address'>
                <div className='dnavbar'>
                    <span onClick={() => { this.onClick() }}><LeftOutline /></span>
                    <div>地址列表</div>
                    <div className='dmicon'>
                        <MoreOutline />
                    </div>
                </div>
                <div className="address_main">
                    {
                        this.state.listadd.map((item, index) => {
                            return (
                                <div className="address_list" key={index} >
                                    <div className="address_list_left" onClick={()=>{this.onfixadd(item,index)}}>
                                        <EditSOutline />
                                    </div>
                                    <div className="address_list_right">
                                        <div className="address_list_textt" onClick={()=>{this.ontitle(item)}}>
                                            <div className="spann">
                                                <span>{item.name}</span>
                                                <span>{item.tel}</span>
                                            </div>
                                            <div className="teextp">
                                            {item.province + item.city + item.county + item.addressDetail}
                                            </div>
                                        </div>
                                        <div className="address_list_icon" onClick={()=>{this.onDeleteadd(item)}}>
                                            <TextDeletionOutline />
                                        </div>

                                    </div>
                                </div>
                            )
                        })
                    }
                    <div className="address_add">
                        <button onClick={() => { this.onaddress_add() }}>新增地址</button>
                    </div>
                </div>

            </div>
        );
    }
}

export default Address;